package com.example.d1216;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText bianhao;
    private Button chaxu;
    private Button chongzhi;
    private Button liebiao;
    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LitePal.getDatabase();
        LitePal.deleteAll(User.class);
        //模拟数据
        List<User> list = new ArrayList<>();
        User user = new User();
        user.setKahao(20001201+"");
        user.setKaming("张志惠");
        user.setKayuer(50);
        list.add(user);
        user = new User();
        user.setKahao(20001202+"");
        user.setKaming("张志豪");
        user.setKayuer(150);
        list.add(user);
        user = new User();
        user.setKahao(20001203+"");
        user.setKaming("何显斌");
        user.setKayuer(500);
        list.add(user);
        user = new User();
        user.setKahao(20001204+"");
        user.setKaming("沈威");
        user.setKayuer(350);
        list.add(user);
        user = new User();
        user.setKahao(20001205+"");
        user.setKaming("杨贵忠");
        user.setKayuer(990);
        list.add(user);
        LitePal.saveAll(list);
        sp=getSharedPreferences("bainhao",MainActivity.MODE_PRIVATE);
        initView();
    }

    private void initView() {
        bianhao = (EditText) findViewById(R.id.bianhao);
        //回显密码
        bianhao.setText(sp.getString("bianhao",null));
        chaxu = (Button) findViewById(R.id.chaxu);
        chongzhi = (Button) findViewById(R.id.chongzhi);
        liebiao = (Button) findViewById(R.id.liebiao);
        chaxu.setOnClickListener(this);
        chongzhi.setOnClickListener(this);
        liebiao.setOnClickListener(this);
    }
    private Boolean booleanUser(){
        //判断编号是否存在
        List<User> all = LitePal.findAll(User.class);
        for (User user:all){
            if(user.getKahao().equals(bianhao.getText().toString().trim())){
                return true;
            }
        }
        return false;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chaxu:
                if(booleanUser()) {
                    //带参跳转到查询页面
                    Intent intent = new Intent(MainActivity.this, ChaXuActivity.class);
                    intent.putExtra("signX", bianhao.getText().toString());
                    //保存密码
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("bianhao", bianhao.getText().toString());
                    edit.commit();
                    startActivity(intent);
                }else {
                    Toast.makeText(MainActivity.this, "编号不存在", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.chongzhi:
                if(booleanUser()) {
                    //带参跳转到充值页面
                    Intent intent1 = new Intent(MainActivity.this,ChongZhiActivity.class);
                    intent1.putExtra("signC",bianhao.getText().toString());
                    startActivity(intent1);
                    //保存密码
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("bianhao", bianhao.getText().toString());
                    edit.commit();
                }else {
                    Toast.makeText(MainActivity.this, "编号不存在", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.liebiao:
                //跳转到列表页面
                startActivity(new Intent(MainActivity.this,LieBiaoActivity.class));
                break;
        }
    }
}
